let PlayerData = require("PlayerData");

function objectToQuerystring(obj) {
  return Object.keys(obj).reduce(function (str, key, i) {
    let delimiter, val;
    delimiter = (i === 0) ? '' : '&';
    key = encodeURIComponent(key);
    val = obj[key];
    if (typeof val === 'object') {
      val = JSON.stringify(val);
    } else {
      val = encodeURIComponent(val);
    }
    
    return [str, delimiter, key, '=', val].join('');
  }, '');
}

function makeShareQueryString(shareParams) {
  if (shareParams === undefined) {
    return;
  }
  let action = shareParams.action;
  delete shareParams.action;
  let query = objectToQuerystring(shareParams);
  if (PlayerData.instance.getUserId() !== undefined) {
    if (query !== '') {
      query += '&';
    }
    query += 'src_uid=' + PlayerData.instance.getUserId() + '&action=' + action;
  }
  console.log("makeShareQueryString:", query);
  return query;
}

module.exports = makeShareQueryString;